#define  _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>

#define REALLOC 5
#define MAX 10

typedef int StackType;

typedef struct IntegrateStack
{
	StackType* base;
	StackType top;
	int StackSize;
}Stack;

int IsEmptyStack(Stack* s);

void InitStack(Stack* s)
{
	assert(s);
	s->base = NULL;
	s->top = 0;
	s->StackSize = 0;
}

void PushStack(Stack* s, StackType x)
{
	assert(s);
	if (s->top == s->StackSize)
	{
		int newStackSize = s->StackSize == 0 ? MAX : s->StackSize + REALLOC;
		StackType* tmp = (StackType*)realloc(s->base, sizeof(StackType) * newStackSize);
		if (tmp == NULL)
		{
			return;
		}
		s->StackSize = newStackSize;
		s->base = tmp;
	}
	s->base[s->top] = x;
	s->top++;
}

void PopStack(Stack* s)
{
	assert(!IsEmptyStack(s));
	s->top--;
}

StackType TopStack(Stack* s)
{
	assert(!IsEmptyStack(s));
	assert(s);
	return s->base[s->top - 1];
}

int StackSize(Stack* s)
{
	return s->top;
}

void DestroyStack(Stack* s)
{
	free(s->base);
	s->base = NULL;
	s->top = 0;
	s->StackSize = 0;
}

int IsEmptyStack(Stack* s)
{
	if (s->top == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

void Print(Stack* s)
{
	int i = 0;
	for (i = 0; i < s->top; i++)
	{
		printf("%d ", s->base[i]);
	}
	printf("\n");
}

int main()
{
	Stack s;
	InitStack(&s);
	PushStack(&s, 1);
	PushStack(&s, 2);
	PushStack(&s, 3);
	PushStack(&s, 4);
	Print(&s);
	PopStack(&s);
	Print(&s);
	printf("%d\n", StackSize(&s));
	PushStack(&s, 5);
	Print(&s);
	printf("%d\n", StackSize(&s));
	printf("%d\n", TopStack(&s));
	DestroyStack(&s);
	return 0;
}